package com.billlv.codegenerator.repository;

import com.billlv.codegenerator.domain.entity.UserTokenEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.time.Instant;
import java.util.List;

@Repository
public interface UserTokenRepository extends JpaRepository<UserTokenEntity, Long> {

    List<UserTokenEntity> findByUserId(Long userId);

    @Modifying
    @Query("DELETE FROM UserTokenEntity ut WHERE ut.expiryTime < :expiryTime")
    void deleteByExpiryTimeBefore(Instant expiryTime);

    @Modifying
    @Query("DELETE FROM UserTokenEntity ut WHERE ut.userId = :userId")
    void deleteByUserId(Long userId);
}
